/**
 * 
 */
package org.swing.utility.jai.magic;

import java.awt.Dimension;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.swing.utility.jai.exception.MagickApiException;
import org.swing.utility.jai.exception.MagickException;
import org.swing.utility.jai.magic.info.DrawInfo;
import org.swing.utility.jai.magic.info.ImageInfo;
import org.swing.utility.jai.magic.info.MagickImage;
import org.swing.utility.jai.magic.info.PixelPacket;
import org.swing.utility.jai.magic.interfaces.CompositeOperator;
import org.swing.utility.jai.magic.interfaces.CompressionType;
import org.swing.utility.jai.magic.interfaces.PreviewType;

/**
 * @author Quynh Nhu
 * 
 */
public class JmagicUtil {
	static {
		System.setProperty("jmagick.systemclassloader", "no");
	}

	public static void addTextToImage(String sourcePath, String targetPath,
			String text, int x, int y, String font, int fontSize,
			String fontColor) throws MagickException {
		ImageInfo info = null;
		MagickImage image = null;
		Dimension imageDim = null;
		MagickImage scaleImg = null;
		int xx = 20;
		int yy = 5;
		if (x > 0) {
			xx = x;
		}
		if (y > 0) {
			yy = y;
		}

		try {
			info = new ImageInfo(sourcePath);
			image = new MagickImage(info);
			imageDim = image.getDimension();
			int w = imageDim.width;
			int h = imageDim.height;
			int a = 0;
			int b = 0;
			String[] as = text.split("");
			for (String string : as) {
				if (string.matches("[\u4E00-\u9FA5]")) {
					a++;
				}
				if (string.matches("[a-zA-Z0-9]")) {
					b++;
				}
			}
			int tl = a * 12 + b * 6 + xx;
			if (w > tl && h > yy) {
				DrawInfo aInfo = new DrawInfo(info);
				aInfo.setOpacity(50);// 0-100
				aInfo.setPointsize(fontSize);
				aInfo.setFont(font);
				aInfo.setFill(PixelPacket.queryColorDatabase(fontColor));
				aInfo.setTextAntialias(true);
				aInfo.setText(text);
				aInfo.setGeometry("+" + xx + "+" + yy);
				image.annotateImage(aInfo);
			}
			image.setFileName(targetPath);
			image.writeImage(info);
		} catch (MagickException e) {
			e.printStackTrace();
		} finally {
			if (scaleImg != null) {
				scaleImg.destroyImages();
			}
			if (image != null) {
				image.destroyImages();
			}
		}
	}

	public static int getWidth(String sourcePath) {
		int w = 1;
		MagickImage sourceImage = null;
		try {
			sourceImage = new MagickImage(new ImageInfo(sourcePath));
			Dimension sourceImageDim = sourceImage.getDimension();
			w = sourceImageDim.width;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (sourceImage != null) {
				sourceImage.destroyImages();
			}
		}
		return w;
	}

	public static int getHeight(String sourcePath) {
		int h = 1;
		MagickImage sourceImage = null;
		try {
			sourceImage = new MagickImage(new ImageInfo(sourcePath));
			Dimension sourceImageDim = sourceImage.getDimension();
			h = sourceImageDim.height;
		} catch (Exception e) {
			// e.printStackTrace();
		} finally {
			if (sourceImage != null) {
				sourceImage.destroyImages();
			}
		}
		return h;
	}

	public static void resizeImage(String sourcePath, String targetPath,
			int size) throws MagickException {
		ImageInfo info = null;
		MagickImage image = null;
		Dimension imageDim = null;
		MagickImage scaleImg = null;
		try {
			info = new ImageInfo(sourcePath);
			image = new MagickImage(info);
			imageDim = image.getDimension();
			int w = imageDim.width;
			int h = imageDim.height;
			float per = 0f;
			if (w > size) {
				per = (float) h / w;
				w = size;
				h = (int) (size * per);
			}
			if (h > size) {
				per = (float) w / h;
				h = size;
				w = (int) (size * per);
			}
			scaleImg = image.scaleImage(w, h);
			scaleImg.setFileName(targetPath);
			scaleImg.writeImage(info);
			scaleImg.destroyImages();
		} catch (MagickException e) {
			e.printStackTrace();
		} finally {
			if (scaleImg != null) {
				scaleImg.destroyImages();
			}
			if (image != null) {
				image.destroyImages();
			}
		}
	}

	public static void resizeImage_wh(String sourcePath, String targetPath,
			int width, int height) throws MagickException {
		ImageInfo info = null;
		MagickImage image = null;
		Dimension imageDim = null;
		MagickImage scaleImg = null;
		try {
			info = new ImageInfo(sourcePath);
			image = new MagickImage(info);
			imageDim = image.getDimension();
			int w = imageDim.width;
			int h = imageDim.height;
			float per = 0f;
			if (w > width) {
				per = (float) h / w;
				w = width;
				h = (int) (width * per);
			}
			if (h > height) {
				per = (float) w / h;
				h = height;
				w = (int) (height * per);
			}
			scaleImg = image.scaleImage(w, h);
			scaleImg.setFileName(targetPath);
			scaleImg.writeImage(info);
			scaleImg.destroyImages();
		} catch (MagickException e) {
			e.printStackTrace();
		} finally {
			if (scaleImg != null) {
				scaleImg.destroyImages();
			}
			if (image != null) {
				image.destroyImages();
			}
		}
	}

	public static void resizeImage(String sourcePath, String targetPath, int w,
			int h) throws MagickException {
		ImageInfo info = null;
		MagickImage image = null;
		MagickImage scaleImg = null;
		try {
			info = new ImageInfo(sourcePath);
			image = new MagickImage(info);
			scaleImg = image.scaleImage(w, h);
			scaleImg.setFileName(targetPath);
			scaleImg.writeImage(info);
		} catch (MagickException e) {
			e.printStackTrace();
		} finally {
			if (scaleImg != null) {
				scaleImg.destroyImages();
			}
			if (image != null) {
				image.destroyImages();
			}
		}
	}

	public static void addLogoToImage(String sourcePath, String targetPath,
			String logo, int logo_w, int logo_h, int x, int y)
			throws MagickException {
		ImageInfo info = null;
		MagickImage image = null;
		MagickImage fLogo = null;
		MagickImage sLogo = null;
		try {
			info = new ImageInfo(sourcePath);
			image = new MagickImage(info);
			fLogo = new MagickImage(new ImageInfo(logo));
			sLogo = fLogo.scaleImage(logo_w, logo_h);
			// sLogo.raiseImage(new Rectangle(4, 4), true);
			// sLogo.raiseImage(new Rectangle(0, 0, 5, 5), true);
			image.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, x, y);
			image.setFileName(targetPath);
			image.writeImage(info);
		} catch (MagickException e) {
			e.printStackTrace();
		} finally {
			if (sLogo != null) {
				sLogo.destroyImages();
			}
			if (fLogo != null) {
				fLogo.destroyImages();
			}
			if (image != null) {
				image.destroyImages();
			}
		}
	}

	public static void addShadowToImage(String sourcePath, String targetPath,
			String logo, int logo_w, int logo_h, int x, int y)
			throws MagickException {
		ImageInfo info = null;
		MagickImage image = null;
		MagickImage fLogo = null;
		MagickImage sLogo = null;
		try {
			info = new ImageInfo(sourcePath);
			image = new MagickImage(info);
			fLogo = new MagickImage(new ImageInfo(logo));
			sLogo = fLogo.scaleImage(logo_w, logo_h);
			image.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, x, y);
			image.setFileName(targetPath);
			image.writeImage(info);
		} catch (MagickException e) {
			e.printStackTrace();
		} finally {
			if (sLogo != null) {
				sLogo.destroyImages();
			}
			if (fLogo != null) {
				fLogo.destroyImages();
			}
			if (image != null) {
				image.destroyImages();
			}
		}
	}

	public static void addWatermarkToImage(String sourcePath,
			String targetPath, String logo, int logo_w, int logo_h, int x, int y)
			throws MagickException {
		ImageInfo info = null;
		MagickImage image = null;
		MagickImage fLogo = null;
		MagickImage sLogo = null;
		try {
			info = new ImageInfo(sourcePath);
			image = new MagickImage(info);
			fLogo = new MagickImage(new ImageInfo(logo));
			sLogo = fLogo.scaleImage(logo_w, logo_h);
			image.compositeImage(CompositeOperator.AtopCompositeOp, sLogo, x, y);
			image.setFileName(targetPath);
			image.writeImage(info);
		} catch (MagickException e) {
			e.printStackTrace();
		} finally {
			if (sLogo != null) {
				sLogo.destroyImages();
			}
			if (fLogo != null) {
				fLogo.destroyImages();
			}
			if (image != null) {
				image.destroyImages();
			}
		}
	}

	public static void resetsize(String picFrom, String picTo) {

		try {

			ImageInfo info = new ImageInfo(picFrom);

			MagickImage image = new MagickImage(new ImageInfo(picFrom));

			MagickImage scaled = image.scaleImage(120, 97);

			scaled.setFileName(picTo);

			scaled.writeImage(info);

		} catch (MagickApiException ex) {

			ex.printStackTrace();

		} catch (MagickException ex) {

			ex.printStackTrace();

		}

	}

	public static void createThumbnail(String filePath, String toPath)
			throws MagickException {

		ImageInfo info = null;

		MagickImage image = null;

		Dimension imageDim = null;

		MagickImage scaled = null;

		try {

			info = new ImageInfo(filePath);

			image = new MagickImage(info);

			imageDim = image.getDimension();

			int wideth = imageDim.width;

			int height = imageDim.height;

			if (wideth > height) {

				height = 660 * height / wideth;

				wideth = 660;

			}

			scaled = image.scaleImage(wideth, height);

			scaled.setFileName(toPath);

			scaled.writeImage(info);

		} finally {

			if (scaled != null) {

				scaled.destroyImages();

			}

		}

	}

	public static void initLogoImg(String filePath, String toImg,
			String logoPath) throws MagickException {

		ImageInfo info = new ImageInfo();

		MagickImage fImage = null;

		MagickImage sImage = null;

		MagickImage fLogo = null;

		MagickImage sLogo = null;

		Dimension imageDim = null;

		Dimension logoDim = null;

		try {

			fImage = new MagickImage(new ImageInfo(filePath));

			imageDim = fImage.getDimension();

			int width = imageDim.width;

			int height = imageDim.height;

			if (width > 660) {

				height = 660 * height / width;

				width = 660;

			}

			sImage = fImage.scaleImage(width, height);

			fLogo = new MagickImage(new ImageInfo(logoPath));

			logoDim = fLogo.getDimension();

			int lw = width / 8;

			int lh = logoDim.height * lw / logoDim.width;

			sLogo = fLogo.scaleImage(lw, lh);

			sImage.compositeImage(CompositeOperator.AtopCompositeOp, sLogo,
					width - (lw + lh / 10), height - (lh + lh / 10));

			sImage.setFileName(toImg);

			sImage.writeImage(info);

		} finally {

			if (sImage != null) {

				sImage.destroyImages();

			}

		}

	}

	public static void initTextToImg(String filePath, String toImg, String text , String fontPath)
			throws MagickException {

		ImageInfo info = new ImageInfo(filePath);

		if (filePath.toUpperCase().endsWith("JPG")
				|| filePath.toUpperCase().endsWith("JPEG")) {

			info.setCompression(CompressionType.JPEGCompression);

			info.setPreviewType(PreviewType.JPEGPreview);

			info.setQuality(95);

		}

		MagickImage aImage = new MagickImage(info);

		Dimension imageDim = aImage.getDimension();

		int wideth = imageDim.width;

		int height = imageDim.height;

		if (wideth > 660) {

			height = 660 * height / wideth;

			wideth = 660;

		}

		int a = 0;

		int b = 0;

		String[] as = text.split("");

		for (String string : as) {

			if (string.matches("[\u4E00-\u9FA5]")) {

				a++;

			}

			if (string.matches("[a-zA-Z0-9]")) {

				b++;

			}

		}

		int tl = a * 12 + b * 6 + 300;

		MagickImage scaled = aImage.scaleImage(wideth, height);

		if (wideth > tl && height > 5) {

			DrawInfo aInfo = new DrawInfo(info);

			aInfo.setFill(PixelPacket.queryColorDatabase("white"));

			aInfo.setUnderColor(new PixelPacket(0, 0, 0, 100));

			aInfo.setPointsize(12);

			fontPath = "C:/WINDOWS/Fonts/MSYH.TTF";

			// String fontPath = "/usr/maindata/MSYH.TTF";

			aInfo.setFont(fontPath);

			aInfo.setTextAntialias(true);

			aInfo.setOpacity(0);

			aInfo.setText("　" + text + " image　"
					+ new SimpleDateFormat("yyyy-MM-dd").format(new Date())
					+ "　invalid !!");

			aInfo.setGeometry("+" + (wideth - tl) + "+" + (height - 5));

			scaled.annotateImage(aInfo);

		}

		scaled.setFileName(toImg);

		scaled.writeImage(info);

		scaled.destroyImages();

	}
	


}
